﻿using FileUploadAPI.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.IO;
using static FileUploadAPI.Model.UploadModel;

namespace FileUploadAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FileUploadController : ControllerBase
    {
        private readonly PzdjyContext db;
        public FileUploadController(PzdjyContext c)
        {
            db = c;
        }
        /// <summary>
        /// 一注二注电解液记录上传图片
        /// </summary>
        /// <param name="uploadModel"></param>
        /// <param name="id"></param>
        [HttpPost("{id}")]
        public async Task<object> N1(IFormFile uploadModel, int id)
        {
            string path = $"{DateTime.Now.Ticks}.jpg";
            if (uploadModel.FileName.ToLower().EndsWith("jpg") || uploadModel.FileName.ToLower().EndsWith("png"))
            {
                var yz = await db.一注二注电解液记录s.FirstOrDefaultAsync(x => x.Id == id);
                if (yz == null)
                {
                    return BadRequest(new { state = 100 });
                }
                using (Stream stream = uploadModel.OpenReadStream())
                {
                    using (FileStream fs = new FileStream($"pic\\{path}", FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        stream.CopyTo(fs);
                        yz.图片 = path;
                        await db.SaveChangesAsync();
                        return Ok(new { state = 200 });
                    }
                }
            }
            else
            {
                return BadRequest(new { state = 400 });
            }
        }

        /// <summary>
        /// 电解液磁杂测试记录
        /// </summary>
        /// <param name="uploadModel">上传图片</param>
        /// <param name="id">数据库中的序号</param>
        /// <param name="type">文件类型，两张图，1是电解液图片，2是取样图片</param>
        /// <returns></returns>
        [HttpPost("{id}/{type}")]
        public async Task<object> N2(IFormFile uploadModel, int id, int type)
        {
            string path = $"{DateTime.Now.Ticks}.jpg";
            if (uploadModel.FileName.ToLower().EndsWith("jpg") || uploadModel.FileName.ToLower().EndsWith("png"))
            {
                var djy = await db.电解液磁杂测试记录s.FirstOrDefaultAsync(x => x.Id == id);
                if (djy == null)
                {
                    return BadRequest(new { state = 100 });
                }
                using (Stream stream = uploadModel.OpenReadStream())
                {
                    using (FileStream fs = new FileStream($"pic\\{path}", FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        stream.CopyTo(fs);
                        string s = type switch
                        {
                            1 => djy.电解液图片 = path,
                            2 => djy.取样图片 = path,
                        };
                        await db.SaveChangesAsync();
                        return Ok(new { state = 200 });
                    }
                }
            }
            else
            {
                return BadRequest(new { state = 400 });
            }
        }


        [HttpPost("{id}")]
        public async Task<object> N3(IFormFile uploadModel, int id)
        {
            string path = $"{DateTime.Now.Ticks}.jpg";
            if (uploadModel.FileName.ToLower().EndsWith("jpg") || uploadModel.FileName.ToLower().EndsWith("png"))
            {
                var sf = await db.水分测试仪标定记录s.FirstOrDefaultAsync(x => x.Id == id);
                if (sf == null)
                {
                    return BadRequest(new { state = 100 });
                }
                using (Stream stream = uploadModel.OpenReadStream())
                {
                    using (FileStream fs = new FileStream($"pic\\{path}", FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        stream.CopyTo(fs);
                        sf.测试图片 = path;
                        await db.SaveChangesAsync();
                        return Ok(new { state = 200 });
                    }
                }
            }
            else
            {
                return BadRequest(new { state = 400 });
            }
        }
    }
}
